from forest.api import Command, get_tool

class Service(Command):
    ''' Restarts services if specified files have been changed
    '''
    __register__ = 'commands.setup.Service'

    def run(self, config, set_default = True):

        installtree = config.get_install_root()

        systemd = get_tool('systemd.Systemd')(installtree)

        for service in config.get_resources().iter_by_type('service'):
            curr_service = systemd.get_service(service.get_name())
            if not curr_service:
                continue

            depends = service.get_depends()

            if not depends:
                return

            for dependant in depends:
                if dependant in config.get_modified():
                    Command.SIGNAL_COMMAND_PROGRESS(self, message='Restarting the service: {}'.format(service.get_name()))
                    curr_service.restart(service.get_name())
                    break

